﻿/*
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * See product pages for more information.
 * http://closesudoku.codeplex.com
 * Copyright (C), 2010, hrondra
 */
using System;
using System.Collections.Generic;
using System.Text;
using Fluid.Controls;
using System.Drawing;
using Close.Classes;
using Close.Properties;

namespace Close.controls
{
    /// <summary>
    /// Edit level folder (rename, delete).
    /// </summary>
   class EditLevel : FluidPanel
   {
      FluidTextBox txtLevelName;
      FluidButton btnSave;
      FluidButton btnDelete;
      private FluidHeader header = new FluidHeader();
      ThemeClass themeClass = ThemeClass.Settings;

      #region "Properties" 

      /// <summary>
      /// After import new game.
      /// </summary>
      private bool _newGame = false;
      private byte _levelId;
      private string _levelName;
      
      /// <summary>
      /// After import new game.
      /// </summary>
      public bool NewGame { set { _newGame = value; } }

      /// <summary>
      /// Level name.
      /// </summary>
      public string LevelName
      {
         set
         {
            txtLevelName.Text = value;
            _levelName = value;
         }
      }

      /// <summary>
      /// Level name.
      /// </summary>
      public byte LevelId
      {
         set
         {
            if (value < 4)
            {
               btnSave.Visible = false;
               btnDelete.Visible = false;
               txtLevelName.Enabled = false;
            }
            _levelId = value;

         }
      }
      #endregion

      protected override void InitControl()
      {
         base.InitControl();

         BackColor = Color.Black;
         Anchor = AnchorAll;
         const int h = 32;
         header.Bounds = new Rectangle(0, 0, themeClass.DisplayWidth, h);
         header.Title = Resources.LevelEditTitle;
         header.BackButton.Visible = true;
         header.BackButton.Shape = ButtonShape.Rounded;
         header.BackButton.Text = Resources.GameMnuBack;
         header.BackButton.Click += new EventHandler(BackButton_Click);
         Controls.Add(header);

         FluidLabel lblLevelName = new FluidLabel();
         lblLevelName.Text = Resources.LevelName;
         lblLevelName.ForeColor = System.Drawing.SystemColors.GrayText;
         lblLevelName.Bounds = new Rectangle(h, h + 5, themeClass.DisplayWidth - h - 5, 17);
         Controls.Add(lblLevelName);
         txtLevelName = new FluidTextBox(String.Empty, h, h + 10 + 17, themeClass.DisplayWidth - 2 * h - 10, 20);
         txtLevelName.BackColor = System.Drawing.Color.DimGray;
         Controls.Add(txtLevelName);

         int buttonWith = 50;
         btnDelete = new FluidButton(Resources.DeleteLevel);
         btnDelete.Bounds = new Rectangle(10, themeClass.DisplayHeight - 30, buttonWith, 20);
         btnDelete.Click += btnDelete_Click;
         btnDelete.BackColor = Color.Black;
         btnDelete.ForeColor = Fluid.Classes.Theme.ThemeClass.Settings.NumpadForeColor;
         Controls.Add(btnDelete);

         btnSave = new FluidButton(Resources.SaveLevel);
         btnSave.Bounds = new Rectangle(themeClass.DisplayWidth - buttonWith - 10, themeClass.DisplayHeight - 30, buttonWith, 20);
         btnSave.Click += btnSave_Click;
         btnSave.BackColor = Color.Black;
         btnSave.ForeColor = Fluid.Classes.Theme.ThemeClass.Settings.NumpadForeColor;
         Controls.Add(btnSave);
      }
      
      /// <summary>
      /// Go back - close dialog. Refresh parent only when the dialog was shown after import.
      /// </summary>
      void BackButton_Click(object sender, EventArgs e)
      {
         CloseDialog(_newGame);         
      }

      /// <summary>
      /// Close the dialog.
      /// </summary>
      /// <param name="refresh">With parent refresh.</param>
      void CloseDialog(bool refresh)
      {
         if (EditLevelClosed != null)
            EditLevelClosed(refresh);

         this.Close();
         this.Dispose();
      }

      /// <summary>
      /// Button restart clicked - show confirmation dialog.
      /// </summary>
      void btnDelete_Click(object sender, EventArgs e)
      {
         MessageDialog msgReallyRestart = new MessageDialog();
         msgReallyRestart.Message = String.Format(Resources.DeleteLevelMask, _levelName);
         msgReallyRestart.OkText = Resources.YesLabel;
         msgReallyRestart.CancelText = Resources.NoLabel;
         msgReallyRestart.Result += new EventHandler<Fluid.Classes.DialogEventArgs>(msgReallyDelete_Result);
         msgReallyRestart.ShowModal(ShowTransition.FromBottom);
      }

      /// <summary>
      /// Button restart clicked - show confirmation dialog.
      /// </summary>
      void btnSave_Click(object sender, EventArgs e)
      {
          if (!string.IsNullOrEmpty(txtLevelName.Text))
          {
              //save dataset.
              PuzzleImporter importer = new PuzzleImporter();
              importer.RenameLevel(_levelId, txtLevelName.Text);
              Data.GameDatabase.GamesData.RefreshData();
              CloseDialog(true);
          }
      }

      /// <summary>
      /// Delete games confirmation clicked.
      /// </summary>
      void msgReallyDelete_Result(object sender, Fluid.Classes.DialogEventArgs e)
      {
         switch (e.Result)
         {
            case System.Windows.Forms.DialogResult.OK:
               (new PuzzleImporter()).DeleteLevelFolder(_levelId);
               Data.GameDatabase.GamesData.RefreshData();
               CloseDialog(true);
               break;
         }
      }

      /// <summary>
      /// Panel closed.
      /// </summary>
      /// <param name="e">refresh</param>
      public delegate void EditLevelClosedDelegate(bool e);
      /// <summary>
      /// One second elapsed - return seconds from the begenning
      /// </summary>
      public event EditLevelClosedDelegate EditLevelClosed;

   }
}
